package com.practice.system.service.impl;

import com.practice.system.dto.BomDto;
import com.practice.system.dto.BomUpdateDto;
import com.practice.system.mapper.BomMapper;
import com.practice.system.mapper.MaterielMapper;
import com.practice.system.mapper.PAndmMapper;
import com.practice.system.mapper.ProductMapper;
import com.practice.system.pojo.Bom;
import com.practice.system.pojo.Materiel;
import com.practice.system.pojo.Product;
import com.practice.system.pojo.pAndm;
import com.practice.system.service.BomService;
import com.practice.utils.PageUtils;
import com.practice.utils.Query;
import com.practice.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class BomServiceImpl implements BomService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    PAndmMapper pAndmMapper;
    @Autowired
    MaterielMapper materielMapper;
    @Autowired
    BomMapper bomMapper;

    @Override
    public R selectBomById(Integer id) {
        //获取bom数据
        Bom bom = bomMapper.getBomById(id);
        //获取产品信息
        String productId = bom.getProductId();
        if (productId == null) {
            return R.ok().put("data", bom);
        }
        Product product = productMapper.selectById(productId);
        //根据中间表获取全部物料id
        List<String> mids = pAndmMapper.getMaterielIds(productId);
        BomUpdateDto bomDto = new BomUpdateDto();
        bomDto.setDescription(bom.getDescription());
//        bomDto.setDosage(bom.getDosage());
        bomDto.setBomName(bom.getBomName());
        bomDto.setProduct(product);
        if (mids.size() == 0) {
            bomDto.setMateriels(null);
            return R.ok().put("data", bomDto);
        }
        //根据获取的ids查询物料信息
        List<Materiel> materielList = materielMapper.getMaterilListByIds(mids);
        bomDto.setMateriels(materielList);
        return R.ok().put("data", bomDto);
    }

    @Override
    public R addBomAddDto(Bom bom) {
        Integer b = bomMapper.addBom(bom);
        if (b == 1) {
            return R.ok("新增成功！");
        }
        return R.error("新增失败！");
    }

    @Override
    public R list(Map<String, Object> params) {
        new Query().setStartPage(params);
        List<Bom> boms = bomMapper.list(params);
        //查询全部产品
        List<Product> products = productMapper.selectAll();
        List<BomDto> collect = boms.stream().map(bom -> {
            BomDto bomDto = new BomDto();
            //根据产品id查询数据
            List<Product> productList = products.stream().map(product -> {
                if (Objects.equals(product.getId(), bom.getProductId())) {
                    return product;
                }
                return null;
            }).filter(Objects::nonNull).collect(Collectors.toList());
            if (productList.size() > 0) {
                BeanUtils.copyProperties(productList.get(0), bomDto);
            }
            BeanUtils.copyProperties(bom, bomDto);
            return bomDto;
        }).collect(Collectors.toList());
        Integer mun = bomMapper.bomCount(params);
        PageUtils<BomDto> list = new PageUtils<>(collect, mun);
        return R.ok("查询成功！").put("data", list);
    }

    @Override
    public R updateBom(BomUpdateDto bomUpdateDto) {
        System.out.println(bomUpdateDto);
        Bom bom = new Bom();
        BeanUtils.copyProperties(bomUpdateDto, bom);
        String pid = bomUpdateDto.getProduct().getId();
        if (pid == null){
            pid = "";
        }
        bom.setProductId(pid);
        Integer i = bomMapper.updateBom(bom);
        List<String> mids = bomUpdateDto.getMids();
        if (mids.size() > 0) {
            //先删除关联的数据
            pAndmMapper.deleteByProductId(pid);
            String finalPid = pid;
            List<pAndm> pAndms = mids.stream().map(mid -> {
                pAndm pAndm = new pAndm();
                pAndm.setMaterielId(mid);
                pAndm.setProductId(finalPid);
                return pAndm;
            }).collect(Collectors.toList());
            //再新增数据
            pAndmMapper.addValues(pAndms);
        }
        //修改产品
        if (i == 1) {
            return R.ok("修改成功！");
        }
        return R.error("新增失败！");
    }

    @Override
    public R delete(Integer id) {
      Boolean b =  bomMapper.deleteById(id);
      if (b){
          return R.ok("删除成功！");
      }
      return R.error("删除失败！");
    }
}
